|
|
|
|
|
|
|
Launching Other Programs with the Shell Function |
|
|
|
|
|
|
|
|
From time to time, you may need to launch another Windows program from your Visual Basic code. For example, you could use Visual Basic to schedule a data transfer or file cleanup. You could easily do so by checking the current system time and then running the secondary program when appropriate. You can run other programs by using the Shell function. The syntax for Shell is as follows: |
|
|
|
|
|
|
|
|
DoubleVar = Shell(pathname[,windowstyle]) |
|
|
|
|
|
|
|
|
The following lines of Visual Basic code use Shell to run Notepad and bring up a text file called README.TXT: |
|
|
|
|
|
|
|
|
Dim dTaskID As Double
dTaskID = Shell("Notepad D:\readme.txt", vbNormalFocus) |
|
|
|
|
|
|
|
|
The first parameter of Shell is the command to be executed and the second, optional parameter is the window style. In the preceding example, the constant vbNormalFocus tells the Shell statement to run Notepad in a normal window with focus. The constant specifies two attributes of the new window: style and focus. Style describes how the window will be displayed (normal, minimized, maximized or hidden), and the focus describes whether the application will become the foreground application (only one running application can have the focus). The default window style is minimized with focus. This means the shelled application does not obscure another application's window, but the shelled program has the focus. |
|
|
|
|
|
|
|
|
Launching another application with Shell is easy. If you need to wait for it to complete before your Visual Basic code continues, you need to use slightly more complex code with some additional Windows API calls. This specific API example is discussed in Chapter 20, "Accessing the Windows API." |
|
|
|
|
|
|
|
|
See "Waiting on a Program to Finish Running," p. 461 |
|
|
|
|
|
|
|
|
Locating Files Relative to Your Application |
|
|
|
|
|
|
|
|
When you work with files in Visual Basic, "hard-coding" a file or folder name within your program is never a good idea. A commercial application such as Microsoft Office lets you change the default installation directory to anything you want. Even if you install the application in an off-the-wall location like D:\JUNK123\, it still can find all its other files (such as templates and clip art) and function normally. |
|
|
|
|
|
|
|
|
Likewise, your applications will be more successful and professional if they can survive on any drive or installation path. If you include files beyond the actual program EXE (like a database), you can add some minor coding so that your program can locate these files in a manner that is transparent to the user. |
|
|
|
|
|
|
|
|
Figure 21.2 shows an album selection screen. This program allows the user to click an album image to play the album. The program is designed to run from a CD-ROM drive. This means the path to the program might be D:\TUNES\TUNES.EXE if your CD-ROM drive letter is D. The album cover pictures and database information are also stored in the \TUNES subdirectory on the CD-ROM. |
|
|
|
|
|